﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using MedicalCare.EntityDefinitions.Entities;
using MedicalCare.Messages;
using MedicalCare.Translator;
using NHibernate;
using NHibernate.Criterion;
using NHibernate.Transform;
using StructureMap;

namespace MedicalCare.DbLayer.Adaptors
{
    public class EquipmentTypeAdaptor : IEquipmentTypeAdaptor
    {
        private ISession _mcSession;

        public EquipmentTypeAdaptor()
        {
            _mcSession = ObjectFactory.GetNamedInstance<ISession>("MedicalCareSession");
        }

        public EquipmentTypeEntity GetEquipmentById(int id)
        {
            try
            {
                var equipmentType = _mcSession.CreateCriteria<EquipmentTypeEntity>()
                    .Add(Restrictions.Eq("Id", id))
                    .List<EquipmentTypeEntity>().FirstOrDefault<EquipmentTypeEntity>();

                return equipmentType;
            }
            catch (Exception ex)
            {

            }
            return null;
        }

        public int DeleteEquipmentTypeById(EquipmentTypeMessage etm)
        {

            try
            {
                int result = _mcSession.CreateSQLQuery(@" EXEC EquipmentType_Delete @Id=:Id,@ModifyUser=:ModifyUser,@Valid=:Valid ")
                           .SetInt32("Id", etm.Id)
                           .SetString("ModifyUser", etm.ModifyUser)
                           .SetString("Valid", etm.Valid)
                           .ExecuteUpdate();

                return result;
            }
            catch (Exception ex)
            {

            }
            return 0;
        }

        public IList<EquipmentTypeEntity> GetAllEquipmentType()
        {
            try
            {
                var list = _mcSession.CreateCriteria<EquipmentTypeEntity>()
                    .Add(Restrictions.Eq("Valid", "Y"))
                    .List<EquipmentTypeEntity>();

                return list;
            }
            catch (Exception ex)
            {
                //return false;
            }
            return null;
        }

        public IList<EquipmentTypeEntity> GetPagingRecords(string orderKey, string orderVal,
                                  int pageIndex, int pageSize, string whereStr)
        {
            try
            {
                //var translator = ObjectFactory.GetInstance<IUserInfoToUserMessage>();
                var query = _mcSession.CreateSQLQuery(@" EXEC Equipments_Paging_GET @PAGEINDEX=:PAGEINDEX, @PAGESIZE=:PAGESIZE, @ORDERKEY=:ORDERKEY, @ORDERVAL=:ORDERVAL, @WHERE=:WHERE ")
                            .SetInt32("PAGEINDEX", pageIndex)
                            .SetInt32("PAGESIZE", pageSize)
                            .SetString("ORDERKEY", orderKey)
                            .SetString("ORDERVAL", orderVal)
                            .SetString("WHERE", whereStr)
                            .SetResultTransformer(Transformers.AliasToBean<EquipmentTypeEntity>());
                var bpList = query.List<EquipmentTypeEntity>();

                return bpList;
            }
            catch (Exception ex)
            {
                //return false;
            }
            return null;
        }

        public int GetRecordsTotal(string whereStr)
        {
            try
            {
                //var translator = ObjectFactory.GetInstance<IUserInfoToUserMessage>();
                var query = _mcSession.CreateSQLQuery(@" EXEC Equipments_Paging_Total @WHERE=:WHERE ")
                          .SetString("WHERE", whereStr)
                          .SetResultTransformer(Transformers.AliasToBean<TotalEntity>());

                var result = query.List<TotalEntity>().FirstOrDefault().Total;

                return result;
            }
            catch (Exception ex)
            {
                //return false;
            }
            return 0;
        }

        public int Update(EquipmentTypeMessage msg)
        {
            var result = default(int);
            try
            {
                if (msg.Id > 0)
                {
                    // TODO: 如果设备id大于0，说明设备已存在，则是更新设备信息
                    result = _mcSession.CreateSQLQuery(@" EXEC EquipmentType_Edit @Id=:Id,@Name=:Name,@Comment=:Comment,@ModifyUser=:ModifyUser")
                           .SetInt32("Id", msg.Id)
                           .SetString("Name", msg.Name)
                           .SetString("Comment", msg.Comment)
                           .SetString("ModifyUser", msg.ModifyUser)
                           .ExecuteUpdate();

                    return result;

                }
                else
                {
                    // TODO: 如果设备id为空/0，则是插入改设备
                    result = _mcSession.CreateSQLQuery(@" EXEC EquipmentType_Insert @Comment=:Comment,@Name=:Name,@CreateUser=:CreateUser,@Valid=:Valid")
                       .SetString("Name", msg.Name)
                       .SetString("Comment", msg.Comment)
                       .SetString("CreateUser", msg.CreateUser)
                       .SetString("Valid", msg.Valid)
                       .ExecuteUpdate();

                    return result;
                }
            }
            catch (Exception ex)
            {
                result = -1;
            }

            return result;
        }

        public void Remove(EquipmentTypeMessage opp)
        {
            throw new NotImplementedException();
        }

        public EquipmentTypeMessage Read(string id)
        {
            throw new NotImplementedException();
        } 
    }
}
